<template>
  <div>
    <el-card>
      <el-input style="width: 200px" placeholder="请输入..." v-model="name"></el-input>
      <el-button style="margin-left: 10px;" plain type="success" @click="load">查询</el-button>
      <el-button type="info" @click="reset" plain>重置</el-button>
      <el-button plain type="primary" @click="handleAdd">新增</el-button>
    </el-card>

    <el-card style="margin: 10px 0">
      <el-table :data="tableData" stripe :header-cell-style="{background:'aliceblue',color:'#666'}">
        <el-table-column prop="id" label="序号" width="70" align="center">
          <template slot-scope='scope'>
            <span>{{ (pageNum - 1) * pageSize + (scope.$index + 1) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="标题"></el-table-column>
        <el-table-column align="center" prop="content" label="内容">
          <template slot-scope="scope">
            <el-button plain type="success" @click="view(scope.row)" size="mini">预览</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="user" label="创建人"></el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button plain type="primary" @click="handleEdit(scope.row)" size="mini">编辑</el-button>
            <el-button plain type="danger" size="mini" @click=del(scope.row.id)>删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div style="margin: 10px 0">
        <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
      </div>
    </el-card>

    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="60%" :close-on-click-modal="false"
               @close="destroy">
      <el-form label-width="100px" style="padding-right: 40px" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item prop="name" label="标题">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="content" label="内容">
          <div id="editor"></div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="信息" :visible.sync="dialogFormVisible1" width="60%">
      <div class="w-e-text" v-html="content"></div>
    </el-dialog>
  </div>
</template>

<script>
import E from "wangeditor"

export default {
  name: "Notice",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      form: {},
      dialogFormVisible: false,
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      rules: {
        name: [
          {required: true, message: '请输入必填项', trigger: 'blur'}
        ],
      },
      dialogFormVisible1: false,
      editor: null,
      content: ''
    }
  },
  created() {
    this.load()
  },
  methods: {
    setEditor() {
      this.$nextTick(() => {
        this.editor = new E(`#editor`)
        this.editor.config.uploadImgHeaders = {
          token: this.user.token
        }
        this.editor.config.uploadImgServer = 'http://localhost:9090/file/editor/upload'
        this.editor.config.uploadFileName = 'file'
        this.editor.config.uploadImgHeaders = {
          token: this.user.token
        }
        this.editor.config.uploadVideoServer = 'http://localhost:9090/file/editor/uploadVideo'
        this.editor.config.uploadVideoName = 'file'
        this.editor.config.uploadVideoHeaders = {
          token: this.user.token
        }
        this.editor.create()
      })
    },
    view(row) {
      this.content = row.content
      this.dialogFormVisible1 = true
    },
    destroy() {
      this.editor.destroy()
      this.editor = null
    },
    load() {
      this.$request.get("/notice/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
        }
      }).then(res => {
        this.tableData = res.data?.records
        this.total = res.data?.total
      })
    },
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.form.content = this.editor.txt.html()
          this.$request({
            method: this.form.id ? 'PUT' : 'POST',
            url: "/notice",
            data: this.form
          }).then(res => {
            if (res.code === '200') {
              this.$message.success("操作成功")
              this.dialogFormVisible = false
              this.load()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}

      // 设置富文本
      this.setEditor()
      this.destroy()
    },
    handleEdit(row) {
      console.log(this.user)
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogFormVisible = true
      // 设置富文本
      this.setEditor()
      setTimeout(() => {
        this.editor.txt.html(row.content) //设置内容
      }, 0)
      this.destroy()
    },
    del(id) {
      this.$confirm('您确认批量删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete("/notice/" + id).then(res => {
          if (res.code === '200') {
            this.$message.success("操作成功")
            this.load()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
      })
    },
    reset() {
      this.name = ""
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleImgUploadSuccess(res) {
      this.form.img = res
    },
  }
}
</script>


<style>

</style>
